<?php

namespace App\Console\Commands;

use App\Model\Device;
use App\Model\InverterData;
use App\Model\InverterDataThree;
use App\Model\InverterDataTwo;
use App\Model\RecordDevicesDayGenCap;
use App\Model\Site;
use App\Model\StatisticsDeviceYearEnergy;
use App\Model\StatisticsDeviceYearEnergyCopy;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RecordRepair extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'RecordRepair';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '修复';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */

    public function handle(){
        $table = ['one','two','three','four','five','six','seven','eight',
            'nine','ten','eleven','twelve','thirteen','fourteen', 'fifteen','sixteen','seventeen','eighteen','nineteen', 'twenty','twentyone','twentytwo','twentythree','twentyfour','twentyfive', 'twentysix','twentyseven','twentyeight', 'twentynine', 'thirty','thirtyone'];

        $month = date('m');
        $year  = date('Y');
//        $sites =  Site::whereIn('status',[1,2])->get();
        $sites = Site::whereIn('id',[33])->get();
//        $filed = intval(date('d',strtotime($begin))) -1;
        foreach ($sites as $site) {
            $fileds = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27];
            foreach ( $fileds as $filed) {
                $day = $filed + 1;
                if ($filed < 9) {

                    $begin = date('Y-m-').'0'.$day.' 23:59:59';
                }else{
                    $begin = date('Y-m-').$day.' 23:59:59';
                }
                $index = $table[$filed];

                $id = $site->id;
                $devices = Device::where('site_id', $id)->get(['device_id']);
                $results = [];
                switch ($id) {
                    case $id < 18:
                        foreach ($devices as $device) {
                            $device_id = $device->device_id;
                            $obj = InverterData::where('site_id', $site->id)->where('device_id', $device_id)->where('DayGenCap','<>',0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();
                            if ($obj) {
                                $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                            } else {
                                $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => 0];
                            }
                            array_push($results, $device_info);
                        }
                        break;
                    case $id >= 18 && $id < 43:
                        foreach ($devices as $device) {
                            $device_id = $device->device_id;
                            $obj = InverterDataTwo::where('site_id', $site->id)->where('device_id', $device_id)->where('DayGenCap','<>',0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();
                            if ($obj) {
                                $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                            } else {
                                $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => 0];
                            }
                            array_push($results, $device_info);
                        }
                        break;
                    default:
                        foreach ($devices as $device) {
                            $device_id = $device->device_id;
                            $obj = InverterDataThree::where('site_id', $site->id)->where('device_id', $device_id)->where('DayGenCap','<>',0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();

                            if ($obj) {
                                $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                            } else {
                                $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => 0];
                            }
                            array_push($results, $device_info);
                        }
                }

                foreach ($results as $result) {
                    $param['site_id'] = $result['site_id'];
                    $param['device_id'] = $result['device_id'];
                    $param[$index] = $result[$index];
                    $param['month'] = $month;
                    $param['year'] = $year;

                    $record = RecordDevicesDayGenCap::where('site_id', $result['site_id'])
                        ->where('device_id', $result['device_id'])
                        ->where('month', $month)
                        ->where('year', $year)
                        ->first();
                    if ($record) {
                        if($record->$index ==0) {
                            $record->update($param);
                        }
                    } else {
                        RecordDevicesDayGenCap::create($param);
                    }
                    unset($param);
                }
            }
        }
    }

//    public function  handle()
//    {
//        $begin = date('Y-m-d') . '23:59:59';
////        $begin = "2020-10-17 19:59:59";
//        $table = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
//            'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twentyone', 'twentytwo', 'twentythree', 'twentyfour', 'twentyfive', 'twentysix', 'twentyseven', 'twentyeight', 'twentynine', 'thirty', 'thirtyone'];
//
//        $month = date('m');
//        $year = date('Y');
////        $sites =  Site::whereIn('status',[1,2])->get();
//        $sites = [1, 3, 5, 7, 9, 10, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 25, 27, 28, 29, 31, 33,34,35, 37, 38,41, 43, 44, 45,46, 47, 48, 50, 51, 52, 54, 55,57, 58, 59,60,61];
////        $sites = [60,61];
////        $sites = [34,41,46,55];
//        $filed = intval(date('d', strtotime($begin))) - 1;
////        $filed = 15;
//        $index = $table[$filed];
//        $results = [];
//        foreach ($sites as $site) {
////            $id = $site->id;
//            $id = $site;
//            $devices = Device::where('site_id', $id)->get(['device_id']);
//            switch ($id) {
//                case $id < 18:
//                    foreach ($devices as $device) {
//                        $device_id = $device->device_id;
//                        $obj = InverterData::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();
//                        if ($obj) {
//                            if (date('Y-m-d', strtotime($obj->EventTime)) == date('Y-m-d')) {
//                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//                            } else {
//                                $device_info = ['site_id' =>$id, 'device_id' => $device_id, $index => 0];
//                            }
////                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//
//                        } else {
//                            $device_info = ['site_id' =>$id, 'device_id' => $device_id, $index => 0];
//                        }
////                        $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//                        array_push($results, $device_info);
//                    }
//                    break;
//                case $id >= 18 && $id < 43:
//                    foreach ($devices as $device) {
//                        $device_id = $device->device_id;
//                        $obj = InverterDataTwo::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();
//                        if ($obj) {
//                            if (date('Y-m-d', strtotime($obj->EventTime)) == date('Y-m-d')) {
//                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//                            } else {
//                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => 0];
//                            }
////                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//
//                        } else {
//                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => 0];
//                        }
////                        $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//                        array_push($results, $device_info);
//                    }
//                    break;
//                default:
//                    foreach ($devices as $device) {
//                        $device_id = $device->device_id;
//                        $obj = InverterDataThree::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();
//
//                        if ($obj) {
//                            if (date('Y-m-d', strtotime($obj->EventTime)) == date('Y-m-d')) {
//                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//                            } else {
//                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => 0];
//                            }
////                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//
//                        } else {
//                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => 0];
//                        }
////                        $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
//                        array_push($results, $device_info);
//                    }
//            }
//            DB::beginTransaction();
////            dump($results);
//            foreach ($results as $result) {
//                $param['site_id'] = $result['site_id'];
//                $param['device_id'] = $result['device_id'];
//                $param[$index] = $result[$index];
//                $param['month'] = $month;
//                $param['year'] = $year;
//
//                $record = RecordDevicesDayGenCap::where('site_id', $result['site_id'])
//                    ->where('device_id', $result['device_id'])
//                    ->where('month', $month)
//                    ->where('year', $year)
//                    ->first();
//                if ($record) {
//                    $record->update($param);
//                } else {
//                    if ($result[$index] != 0) {
//                        RecordDevicesDayGenCap::create($param);
//                    }
//                }
//                unset($param);
//            }
//            DB::commit();
//        }
//    }
}
